home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / TIMEZONE.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  4KB  |  124 lines

  1. /* This is file TIMEZONE.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1987 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms are permitted
  12.  * provided that: (1) source distributions retain this entire copyright
  13.  * notice and comment, and (2) distributions including binaries display
  14.  * the following acknowledgement:  ``This product includes software
  15.  * developed by the University of California, Berkeley and its contributors''
  16.  * in the documentation or other materials provided with the distribution
  17.  * and in all advertising materials mentioning features or use of this
  18.  * software. Neither the name of the University nor the names of its
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  */
  25.  
  26. #if defined(LIBC_SCCS) && !defined(lint)
  27. static char sccsid[] = "@(#)timezone.c    5.9 (Berkeley) 6/1/90";
  28. #endif /* LIBC_SCCS and not lint */
  29.  
  30. #include <sys/types.h>
  31. #include <sys/time.h>
  32. #include <stdio.h>
  33. #include <tzfile.h>
  34.  
  35. /*
  36.  * timezone --
  37.  *    The arguments are the number of minutes of time you are westward
  38.  *    from Greenwich and whether DST is in effect.  It returns a string
  39.  *    giving the name of the local timezone.  Should be replaced, in the
  40.  *    application code, by a call to localtime.
  41.  */
  42.  
  43. static char    czone[TZ_MAX_CHARS];        /* space for zone name */
  44.  
  45. char *
  46. timezone(zone, dst)
  47.     int    zone,
  48.         dst;
  49. {
  50.     register char    *beg,
  51.             *end;
  52.     char    *getenv(), *index(), *strncpy(), *_tztab();
  53.  
  54.     if (beg = getenv("TZNAME")) {        /* set in environment */
  55.         if (end = index(beg, ',')) {    /* "PST,PDT" */
  56.             if (dst)
  57.                 return(++end);
  58.             *end = '\0';
  59.             (void)strncpy(czone,beg,sizeof(czone) - 1);
  60.             czone[sizeof(czone) - 1] = '\0';
  61.             *end = ',';
  62.             return(czone);
  63.         }
  64.         return(beg);
  65.     }
  66.     return(_tztab(zone,dst));    /* default: table or created zone */
  67. }
  68.  
  69. static struct zone {
  70.     int    offset;
  71.     char    *stdzone;
  72.     char    *dlzone;
  73. } zonetab[] = {
  74.     -1*60,    "MET",    "MET DST",    /* Middle European */
  75.     -2*60,    "EET",    "EET DST",    /* Eastern European */
  76.     4*60,    "AST",    "ADT",        /* Atlantic */
  77.     5*60,    "EST",    "EDT",        /* Eastern */
  78.     6*60,    "CST",    "CDT",        /* Central */
  79.     7*60,    "MST",    "MDT",        /* Mountain */
  80.     8*60,    "PST",    "PDT",        /* Pacific */
  81. #ifdef notdef
  82.     /* there's no way to distinguish this from WET */
  83.     0,    "GMT",    0,        /* Greenwich */
  84. #endif
  85.     0*60,    "WET",    "WET DST",    /* Western European */
  86.     -10*60,    "EST",    "EST",        /* Aust: Eastern */
  87.      -10*60+30,    "CST",    "CST",        /* Aust: Central */
  88.     -8*60,    "WST",    0,        /* Aust: Western */
  89.     -1
  90. };
  91.  
  92. /*
  93.  * _tztab --
  94.  *    check static tables or create a new zone name; broken out so that
  95.  *    we can make a guess as to what the zone is if the standard tables
  96.  *    aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
  97.  *    STANDARD LIBRARY.
  98.  */
  99. char *
  100. _tztab(zone,dst)
  101.     register int    zone;
  102.     int    dst;
  103. {
  104.     register struct zone    *zp;
  105.     register char    sign;
  106.  
  107.     for (zp = zonetab; zp->offset != -1;++zp)    /* static tables */
  108.         if (zp->offset == zone) {
  109.             if (dst && zp->dlzone)
  110.                 return(zp->dlzone);
  111.             if (!dst && zp->stdzone)
  112.                 return(zp->stdzone);
  113.         }
  114.  
  115.     if (zone < 0) {                    /* create one */
  116.         zone = -zone;
  117.         sign = '+';
  118.     }
  119.     else
  120.         sign = '-';
  121.     (void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
  122.     return(czone);
  123. }
  124.